package com.qf.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;

/**
 * 演示Cookie跨域
 */
@RestController
public class CookieController {

    @GetMapping("write")
    public String write(HttpServletResponse response) {
        Cookie cookie = new Cookie("name","admin");
        cookie.setMaxAge(60*60*24);

        // 如果Cookie要实现多级域名跨域，这里需要写一级域名
        cookie.setDomain("qf.com"); // 默认是当前域
        response.addCookie(cookie);
        return "write";
    }

    @GetMapping("read")
    public String read(@CookieValue(required = false,defaultValue = "") String name) {
        System.out.println("name:"+name);
        return "read";
    }
}
